//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.highglass.videoplayer.subTitle;
import com.highglass.videoplayer.SubContentUtil;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;


public class SrtDecoder extends SubTitleDecoder {
    private static int count = 0;

    public SrtDecoder() {
    }

    private int getSrtTime(String str) {
        boolean targetTime = false;
        String strHour = str.substring(0, 2);
        String strMinute = str.substring(3, 5);
        String strSecond = str.substring(6, 8);
        String strMicosecond = str.substring(9, 12);
        int hour = Integer.parseInt(strHour);
        int minute = Integer.parseInt(strMinute);
        int second = Integer.parseInt(strSecond);
        int micosecond = Integer.parseInt(strMicosecond);
        int targetTime1 = hour * 3600000 + minute * '\uea60' + second * 1000 + micosecond;
        return targetTime1;
    }

    private boolean isNumberic(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException var3) {
            return false;
        }
    }

    public Map<String, List<SubTitleContent>> decodeSubTitle(String subTitlePath, String encode) throws Exception {
        BufferedReader br = null;
        if(encode == null) {
            br = SubContentUtil.getEncodeType(subTitlePath);
        } else {
            br = SubContentUtil.getEncodeType(subTitlePath, encode);
        }

        String subTitleLine = null;
        String regex = "\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d --> \\d\\d:\\d\\d:\\d\\d,\\d\\d\\d";
        SubTitleContent content = null;
        TreeMap subTitleMap = new TreeMap();
        ArrayList subTitleList = new ArrayList();
        StringBuffer subTitleBuf = new StringBuffer("");
        String preLine = null;
        String subTitleStr = null;
        int languageIndex = 1;
        int preLineNum = 0;
        String subTitle = null;
        boolean starttime = false;
        boolean endtime = false;
        int prestarttime = 0;
        TreeMap subTimeMap = new TreeMap();

        while((subTitleLine = br.readLine()) != null && !SubContentUtil.stopDecodeFlag) {
            subTitleLine = subTitleLine.trim();
            if(count == 0 && "1".equals(subTitleLine)) {
                preLine = subTitleLine;
                preLineNum = 1;
            } else {
                if(Pattern.matches(regex, subTitleLine)) {
                    content = new SubTitleContent();
                    content.setmSubTitleIndex(count++);
                    int var21 = this.getSrtTime(subTitleLine.split("-->")[0].trim());
                    content.setmSubTitleStartTime(var21);
                    int var22 = this.getSrtTime(subTitleLine.split("-->")[1].trim());
                    content.setmSubTitleEndTime(var22);
                    if(var21 == 0 && var22 == 0) {
                        continue;
                    }

                    if(prestarttime > var21) {
                        subTitleMap.put(String.valueOf(languageIndex), subTitleList);
                        ++languageIndex;
                        count = 0;
                        subTitleList = new ArrayList();
                    }

                    content.setmLanguageClass(String.valueOf(languageIndex));
                    if(!subTimeMap.containsKey(Integer.valueOf(var21))) {
                        subTimeMap.put(Integer.valueOf(var21), content);
                    } else if(content.getmLanguageClass().equals(((SubTitleContent)subTimeMap.get(Integer.valueOf(var21))).getmLanguageClass())) {
                        content = (SubTitleContent)subTimeMap.get(Integer.valueOf(var21));
                    }

                    prestarttime = var21;
                } else if(this.isNumberic(subTitleLine) && "".equals(preLine)) {
                    subTitleStr = subTitleBuf.toString().trim();
                    if("".equals(subTitleStr)) {
                        continue;
                    }

                    subTitle = subTitleStr.substring(0, subTitleStr.length() - "<br>".length()).trim();
                    if(content.getmSubTitleLine() != null && content.getmSubTitleLine().trim().length() > 0) {
                        content.setmSubTitleLine(content.getmSubTitleLine() + subTitle);
                    } else {
                        content.setmSubTitleLine(subTitle);
                    }

                    if(!subTitleList.contains(content)) {
                        subTitleList.add(content);
                    }

                    content = null;
                    subTitleBuf.delete(0, subTitleBuf.length());
                    if(Integer.parseInt(subTitleLine) < preLineNum) {
                        subTitleMap.put(String.valueOf(languageIndex), subTitleList);
                        ++languageIndex;
                        count = 0;
                        subTitleList = new ArrayList();
                    }

                    preLineNum = Integer.parseInt(subTitleLine);
                } else if(!"".equals(subTitleLine.trim())) {
                    subTitleBuf.append(subTitleLine + "<br>");
                }

                preLine = subTitleLine;
            }
        }

        subTitleStr = subTitleBuf.toString().trim();
        if(!"".equals(subTitleStr)) {
            subTitle = subTitleStr.substring(0, subTitleStr.length() - "<br>".length()).trim();
            content.setmSubTitleLine(subTitle);
            subTitleList.add(content);
            content = null;
        }

        subTitleMap.put(String.valueOf(languageIndex), subTitleList);

        try {
            br.close();
        } catch (Exception var20) {
            var20.printStackTrace();
        }

        SubContentUtil.SortAllList(subTitleMap);
        return subTitleMap;
    }
}
